package com.swz.dcrm.ui.mine;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.bumptech.glide.Glide;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.util.FileUtils;
import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.widget.CircleImageView;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyIdCardFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.iv_qrCode)
    ImageView qrCode;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_post)
    TextView tvPost;
    @BindView(R.id.tv_store_name)
    TextView tvStoreName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.iv_headPic)
    CircleImageView ivHeadPic;
    @Inject
    MainViewModel mainViewModel;

    private Disposable mDisposable;
    private Bitmap bitmap;
    public static MyIdCardFragment newInstance() {

        return new MyIdCardFragment();
        // Required empty public constructor
    }
    @Override
    public void onLoadRetry() {
        mainViewModel.getPersonalInfo().observe(this,personalInfoBaseResponse -> {
            if(personalInfoBaseResponse.isSuccess()){
                tvName.setText(personalInfoBaseResponse.getData().getName());
                tvPost.setText(personalInfoBaseResponse.getData().getRole().getRoleGroup());
                tvStoreName.setText(personalInfoBaseResponse.getData().getCarShop().getName());
                tvPhone.setText(getString(R.string.my_card_phone,personalInfoBaseResponse.getData().getPhone()));
                if(personalInfoBaseResponse.getData().getNameCardUrl()!=null){
                    bitmap=Tool.generateBitmap(personalInfoBaseResponse.getData().getNameCardUrl(),
                            qrCode.getMeasuredWidth(),qrCode.getMeasuredHeight());
                    qrCode.setImageBitmap(bitmap);
                }else {
                    bitmap=Tool.generateBitmap("123",
                            qrCode.getMeasuredWidth(),qrCode.getMeasuredHeight());
                    qrCode.setImageBitmap(bitmap);
                }
                Glide.with(getActivity())
                        .load(personalInfoBaseResponse.getData().getHeadPortraitUrl())
                        .into(ivHeadPic);
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
    @Override
    public int layoutId() {
        return R.layout.fragment_my_idcard;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.my_card_title));
        ViewTreeObserver vto = qrCode.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {

                onLoadRetry();


                qrCode.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        return true;
    }

    @Override
    public void onDestroyView() {
        if(mDisposable!=null){
            mDisposable.dispose();
        }
        super.onDestroyView();
    }

    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }
    @OnClick(R.id.tv_save)
    public void save(){
        mDisposable = Observable.create((ObservableOnSubscribe<Integer>) e -> {
            FileUtils.saveBitmap(getActivity(),bitmap, "idcard.JPEG");
            e.onNext(1);
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(integer -> showToast( "保存到相册"));
    }
}
